package com.jwaoo.chat.receiver;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.jwaoo.chat.ChatHelpter;
import com.jwaoo.chat.activity.module.fromapp.AppInstance;
import com.jwaoo.chat.session.Extras;

/**
 * Created by jwaoo on 2018/2/8.
 */

public class NotificationBroadcastReceiver extends BroadcastReceiver {

    public static final String TYPE = "type"; //这个type是为了Notification更新信息的，这个不明白的朋友可以去搜搜，很多

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        int type = intent.getIntExtra(TYPE, -1);

        if (type != -1) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(type);
        }

        if (action.equals("notification_clicked")) {
            Log.d("notification"," //处理点击事件");
            doClick();
        }

        if (action.equals("notification_cancelled")) {
            //处理滑动清除和点击删除事件
            Log.d("notification"," //处理滑动清除和点击删除事件");
        }
    }

    private void doClick(){
        Class<?> cls = AppInstance.getInstance().getCurrentActivity();
        Intent intent = null;
        if (cls != null){
            intent = new Intent(ChatHelpter.getContext(), cls);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        }else {
            intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
        }
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        ChatHelpter.getContext().startActivity(intent);
    }
}